<?php

namespace app\admin\model\gift;

use app\admin\model\goods\GoodsModel;
use app\admin\model\Levels;
use app\admin\model\User;
use think\Model;


class Goods extends Model
{


    // 表名
    protected $name = 'gift_goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text'
    ];


    public const TYPE_PT = 100; //普通商品
    public const TYPE_WEEK = 200; //周兑换商品
    public const TYPE_MONTH = 300; //月兑换商品

    public const TYPE_ARRAY = [
        self::TYPE_PT => 'Normal Product',
        self::TYPE_WEEK => 'Weekly Exchange Product',
        self::TYPE_MONTH => 'Monthly Exchange Product',
    ];


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("d F Y H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['update_time'] ?? '');
        return is_numeric($value) ? date("d F Y H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function message()
    {
        return $this->belongsTo('app\admin\model\shop\Message', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function categorys()
    {
        return $this->belongsTo('Categorys', 'categorys_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function getImageAttr($value)
    {
        return Https($value);
    }

    /**
     * Notes: 根据用户信息获取产品
     * User: chenLei
     * Date: 8/25/25
     * @param $user_id
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function levelList($user_id)
    {
        $user = User::where('id', $user_id)->find();

       // $level = Levels::field('level,name')->select();

        $data = [];

        $goodsVoucher = $this->where('categorys_id',1)
            ->field('id,image,name,score,level,categorys_id')->order('level asc')->select();
        foreach ($goodsVoucher as &$value) {
            $value['display'] = 100;
            if (!empty($user) && $user->level >= $value['level']) {
                $value['display'] = 200;
            }
        }

        $goodsPhysical = $this->where('categorys_id',2)
            ->field('id,image,name,score,level,categorys_id')->select();
        foreach ($goodsPhysical as &$value) {
            $value['display'] = 100;
            if (!empty($user) && $user->level >= $value['level']) {
                $value['display'] = 200;
            }
        }

        $goodsWeek = $this->where('categorys_id',4)
            ->field('id,image,name,score,level,categorys_id')->select();
        foreach ($goodsWeek as &$value) {
            $value['display'] = 100;
            if (!empty($user) && $user->level >= 4) {
                $value['display'] = 200;
            }
        }

        $goodsMonth = $this->where('categorys_id',3)
            ->field('id,image,name,score,level,categorys_id')->select();
        foreach ($goodsMonth as &$value) {
            $value['display'] = 100;
            if (!empty($user) && $user->level >= 3) {
                $value['display'] = 200;
            }
        }

        $data[] = [
            'type'=>100,
            'name'=>'Cash Voucher',
            'goods'=>$goodsVoucher
        ];
        $data[] = [
            'type'=>100,
            'name'=>'Food & Merchandise',
            'goods'=>$goodsPhysical
        ];
        $data[] = [
            'type'=>300,
            'name'=>'Monthly Benefit',
            'goods'=>$goodsMonth
        ];
        $data[] = [
            'type'=>200,
            'name'=>'Weekly Benefit',
            'goods'=>$goodsWeek
        ];



        return $data;
    }
}
